import 'package:casdon/core/network/i_api_client.dart';
import 'package:casdon/core/utils/json_parse_utils.dart';
import 'package:casdon/modules/lifeCircle/data/api/life_banner_list_api.dart';
import 'package:casdon/modules/lifeCircle/data/api/life_report_api.dart';
import 'package:casdon/modules/lifeCircle/data/api/life_subject_list_api.dart';
import 'package:casdon/modules/lifeCircle/data/api/life_topic_comment_list_api.dart';
import 'package:casdon/modules/lifeCircle/data/api/life_topic_list_api.dart';
import 'package:casdon/modules/lifeCircle/data/repository/life_repository.dart';
import 'package:casdon/modules/me/data/api/user_center_api.dart';

class LifeRepositoryImpl extends LifeRepository {
  // 字段类型也是抽象接口
  final IApiClient _apiClient;

  LifeRepositoryImpl({required IApiClient apiClient}) : _apiClient = apiClient;

  @override
  Future<List<ResponseLifeBannerListApi>?> getlifeBannerList() async {
    final res = await _apiClient.getTyped<List<ResponseLifeBannerListApi>>(
      '/client-operation/position/getBannerList',
      query: {"type": 3},
      fromJsonT: (json) => parseList(json, ResponseLifeBannerListApi.fromJson),
    );
    return res?.data;
  }

  @override
  Future<List<ResponseLifeSubjectApi>?> getLifeSubjectList(int type) async {
    final res = await _apiClient.getTyped<List<ResponseLifeSubjectApi>?>(
      '/client-operation/life/subject/list',
      query: {"type": type},
      fromJsonT: (json) => parseList(json, ResponseLifeSubjectApi.fromJson),
    );
    return res?.data;
  }

  @override //pageNum=1&pageSize=20&isAsc=desc&id=1858748932190965888
  Future<ResponseLifeTopicListApi?> getTopicSubjectList(
    String id,
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseLifeTopicListApi>(
      '/client-operation/life/topic/subject/list',
      query: {
        "id": id,
        "pageNum": pageNum,
        "pageSize": pageSize,
        "isAsc": "desc",
      },
      fromJsonT: (json) =>
          ResponseLifeTopicListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseLifeTopicListApi?> getTopicFlowList(
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseLifeTopicListApi>(
      '/client-operation/life/topic/follow/list',
      query: {
        "pageNum": pageNum,
        "pageSize": pageSize,
        "isAsc": "desc",
        // "orderByColumn": "createTime",
      },
      fromJsonT: (json) =>
          ResponseLifeTopicListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseLifeTopicListApi?> getTopicRecomendList(
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseLifeTopicListApi>(
      '/client-operation/life/topic/recommend/list',
      query: {"pageNum": pageNum, "pageSize": pageSize, "isAsc": "desc"},
      fromJsonT: (json) =>
          ResponseLifeTopicListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseLifeTopicListApi?> getBloggerListWith({
    required String userId,
    required int pageNum,
    required int pageSize,
  }) async {
    final res = await _apiClient.getTyped<ResponseLifeTopicListApi>(
      '/client-operation/life/topic/blogger/list/{userId}',
      pathParams: {"userId": userId, "pageNum": pageNum, "pageSize": pageSize},
      fromJsonT: (json) =>
          ResponseLifeTopicListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseLifeTopicListApi?> getSelfSubjectList({
    required int pageNum,
    required int pageSize,
  }) async {
    final res = await _apiClient.getTyped<ResponseLifeTopicListApi>(
      '/client-operation/life/topic/my/list',
      query: {"pageNum": pageNum, "pageSize": pageSize},
      fromJsonT: (json) =>
          ResponseLifeTopicListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<LifeTopic?> getTopicDetail({required String topicId}) async {
    final res = await _apiClient.getTyped<LifeTopic>(
      '/client-user/life/topic/detail/{id}',
      pathParams: {"id": topicId},
      fromJsonT: (json) => LifeTopic.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<void> viewTopic({required String topicId}) async {
    await _apiClient.put(
      '/client-user/life/topic/view',
      data: {"topicId": topicId},
    );
  }

  @override
  Future<ResponseUserCenterApi?> getBloggerLifeInfo({
    required String userId,
  }) async {
    final res = await _apiClient.getTyped<ResponseUserCenterApi>(
      '/client-operation/life/blogger/info/{userId}',
      pathParams: {"userId": userId},
      fromJsonT: (json) =>
          ResponseUserCenterApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseLifeTopicListApi?> searchTopicList({
    required String searchText,
    required int pageNum,
    required int pageSize,
  }) async {
    final res = await _apiClient.getTyped<ResponseLifeTopicListApi>(
      '/client-operation/life/search/topic',
      query: {
        "searchText": searchText,
        "pageNum": pageNum,
        "pageSize": pageSize,
        "isAsc": "desc",
        "orderByColumn": "create_time",
      },
      fromJsonT: (json) =>
          ResponseLifeTopicListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseLifeTopicCommentListApi?> getTopicComments({
    required String topicId,
    required int pageNum,
    required int pageSize,
  }) async {
    final res = await _apiClient.getTyped<ResponseLifeTopicCommentListApi>(
      '/client-user/life/comment/list/{topicId}',
      pathParams: {"topicId": topicId},
      query: {"pageNum": pageNum, "pageSize": pageSize},
      fromJsonT: (json) => ResponseLifeTopicCommentListApi.fromJson(
        json as Map<String, dynamic>,
      ),
    );
    return res?.data;
  }

  @override
  Future<List<Map<String, dynamic>>?> getCommentReply({
    required String commentId,
    required int pageNum,
    required int pageSize,
  }) async {
    final res = await _apiClient.getTyped<List<Map<String, dynamic>>>(
      '/client-user/life/comment/reply/{commentId}',
      pathParams: {"commentId": commentId},
      query: {"pageNum": pageNum, "pageSize": pageSize},
      fromJsonT: (json) => parseList<Map<String, dynamic>>(
        json,
        (e) => Map<String, dynamic>.from(e as Map),
      ),
    );
    return res?.data;
  }

  @override
  Future<void> addTopicComment({
    required String topicId,  // 帖子id
    required String content, // 评论内容
    String? parentId,  // 被回复的评论的ID，评论帖子时候为 -1
    String? imageSrc,  // 图片，用逗号隔开
    String? toUserId,  // 被评论用户ID
  }) async {
    await _apiClient.post(
      '/client-user/life/comment/add',
      data: {
        "topicId": topicId,
        if (parentId != null) "parentId": parentId,
        "content": content,
        if (imageSrc != null) "imageSrc": imageSrc,
        if (toUserId != null) "toUserId": toUserId,
      },
    );
  }

  @override
  Future<void> delComment({required String id}) async {
    await _apiClient.delete(
      '/client-user/life/comment/del/{id}',
      pathParams: {"id": id},
    );
  }

  @override
  Future<void> likeTopic({
    required String id,
    required int type,
    required int flag,
  }) async {
    await _apiClient.put(
      '/client-operation/life/like',
      data: {"id": id, "type": type, "flag": flag},
    );
  }

  @override
  Future<void> reportTopic({
    required String topicId,
    required String reportType,
    String? reportReason,
  }) async {
    await _apiClient.post(
      '/client-operation/life/topic/report',
      data: LifeReportRequest(
        topicId: topicId,
        reportType: reportType,
        reportReason: reportReason,
      ).toJson(),
    );
  }

  @override
  Future<void> addTopic({
    required int mediaType,
    required String sid,
    required String content,
    required String mediaSrc,
  }) async {
    await _apiClient.post(
      '/client-operation/life/topic/add',
      data: {
        'mediaType': mediaType,
        'sid': sid,
        'content': content,
        'mediaSrc': mediaSrc,
      },
    );
  }

  @override
  Future<void> delTopic({required String id}) async {
    await _apiClient.delete(
      '/client-operation/life/topic/delete/{id}',
      pathParams: {"id": id},
    );
  }
}
